//
//  LoginView.m
//  Project1.0
//
//  Created by dllo on 15/10/15.
//  Copyright (c) 2015年 吴帅. All rights reserved.
//

#import "WSLoginView.h"

@interface WSLoginView ()

@property (nonatomic, strong) UITextField *userName;

@property (nonatomic, strong) UITextField *passWord;

@end

@implementation WSLoginView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews{

    self.backgroundColor = [UIColor colorWithWhite:0.957 alpha:1.000];
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    CGFloat x = width / 2;
    CGFloat y = height / 3;
    
#pragma mark 返回 1000
    UIButton *returnBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [returnBut setImage:[UIImage imageNamed:@"iconfont-addcollapse"] forState:UIControlStateNormal];
    [returnBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:returnBut];
    [returnBut mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@20);
        make.top.mas_equalTo(@30);
        make.height.width.mas_equalTo(@32);
    }];
    
#pragma mark 头像 1001
    UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    imageButton.frame = CGRectMake(145, 80, 80, 80);
    imageButton.tag = 1001;
    [imageButton setBackgroundImage:[UIImage imageNamed:@"头像.png"] forState:UIControlStateNormal];
    imageButton.layer.cornerRadius = 40;
    //将图片变圆角
    imageButton.layer.masksToBounds = YES;
    [imageButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:imageButton];
    
#pragma mark 输入框 1010 1011
    UIView *textView = [[UIView alloc] init];
    textView.backgroundColor = [UIColor whiteColor];
    textView.center = CGPointMake(x, y);
    [textView.layer setBorderWidth:1.5];
    [textView.layer setBorderColor:[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1].CGColor];
    UIView *aview = [[UIView alloc] initWithFrame:CGRectMake(65, 45, 300, 1)];
    aview.backgroundColor = [UIColor colorWithWhite:0.851 alpha:1.000];
    [textView addSubview:aview];
    [self addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@-1);
        make.top.mas_equalTo(@200);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(@91);
    }];
    
    _userName = [[UITextField alloc] init];
    [textView addSubview:_userName];
    [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@75);
        make.top.mas_equalTo(@0);
        make.right.mas_equalTo(@20);
        make.height.mas_equalTo(@45);
    }];
    
    _passWord = [[UITextField alloc] init];
    [textView addSubview:_passWord];
    [_passWord mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@75);
        make.top.mas_equalTo(@46);
        make.right.mas_equalTo(@20);
        make.height.mas_equalTo(@45);
    }];
    
    NSArray *array = [NSArray arrayWithObjects:@" 手机/邮箱", @" 密码", @"iconfont-zhanghao", @"iconfont-mima", nil];
    NSArray *textFieldArr = [NSArray arrayWithObjects:_userName, _passWord, nil];
    
    for (int i = 0; i < 2; i++) {
    
        UIImageView *textImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:array[2 + i]]];
        textImage.frame = CGRectMake(25, 10 + 45 * i, 25, 25);
        [textView addSubview:textImage];
        
        UITextField *textfield = textFieldArr[i];
        textfield.placeholder = array[i];
        if (i == 1) {
            textfield.secureTextEntry = YES;
        }
    }
    
#pragma mark 登录键 1003
    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    but.frame = CGRectMake(39, 297, 300, 45);
    but.tag = 1003;
    but.backgroundColor = [UIColor colorWithRed:0.082 green:0.565 blue:0.847 alpha:1.000];
    [but.layer setCornerRadius:5];
    [but setTitle:@"登陆" forState:UIControlStateNormal];
    [but addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:but];
    
    UIButton *regist = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:regist];
    [regist mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@5);
        make.top.equalTo(but.mas_bottom).offset(5);
        make.height.mas_equalTo(@20);
        make.width.mas_equalTo(@80);
    }];
    regist.titleLabel.font = [UIFont systemFontOfSize:15];
    [regist setTitle:@"用户注册" forState:UIControlStateNormal];
    [regist setTitleColor:[UIColor colorWithRed:0.11 green:0.55 blue:0.96 alpha:1] forState:UIControlStateNormal];
    [regist addTarget:self action:@selector(registAction:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)buttonAction:(UIButton *)button{
    
    [self.delegate returnAction];
}

- (void)registAction:(UIButton *)button {

    [self.delegate pushRegistViewController];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
    for (int i = 0; i < 2; i++) {
        UITextField *textField = (UITextField *)[self viewWithTag:1010 + i];
        if (textField.isFirstResponder) {
            [textField resignFirstResponder];
        }
    }
    
}

- (void)loginAction:(UIButton *)send {

    if ([_userName.text isEqualToString:@""] || [_passWord.text isEqualToString:@""]) {
        NSLog(@"账号密码为空");
        
        return;
    }
    else {
    
        [self.delegate loginWithUserName:_userName.text passWord:_passWord.text];
    }
}

@end
